package com.ujiuye.controller;


import com.ujiuye.config.DownLoad;
import com.ujiuye.config.MessageConstant;
import com.ujiuye.config.MyFileUtils;
import com.ujiuye.config.Result;
import com.ujiuye.pojo.Ordersetting;
import com.ujiuye.service.IOrderService;
import com.ujiuye.service.IOrdersettingService;
import com.ujiuye.vo.DateVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单设置-比如设置今天可预约人数等信息 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {
    @Reference
    private IOrdersettingService ordersettingService;

     @Value("${upload}")
    private String path;
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        File file = new File(path+"ordersetting_template.xls");
        ResponseEntity<byte[]> download = DownLoad.download(file);
        return download;
    }

    @RequestMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile){
        File file = MyFileUtils.upload(multipartFile, path);
         if (file==null){
             return new Result(false,MessageConstant.ORDERSETTING_FAIL);
         }
        List<Ordersetting> ordersettingList = ExcelToList(file);
         if (ordersettingList.size()==0){
             return new Result(false,"文件没有上传的数据");
         }
        try {
            Result result = ordersettingService.update(ordersettingList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @GetMapping("/{dateTime}")
    public Result dateList(@PathVariable("dateTime") String dateTime){
        try {
            List<DateVo> dateVos = ordersettingService.listDate(dateTime);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,dateVos);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    @PostMapping("updatenumber/{date}/{number}")
    public Result updateNumber(@PathVariable("date") Date date,@PathVariable("number") Integer number){
        try {
            Result result = ordersettingService.updateNumber(date, number);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

    }



//读取excel文件数据
    private List<Ordersetting> ExcelToList(File file){
        //创建一个集合用于存从文件取出的数据
        List<Ordersetting> ordersettingList = new ArrayList<>();
        try {
            //创建一个输出流来读取文件
            FileInputStream fileInputStream = new FileInputStream(file);
            //通过输入流构建一个工作簿对象
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
            //获取表
            XSSFSheet sheetAt = workbook.getSheetAt(0);
            //获取表中能够读取的数据
            int numberOfRows = sheetAt.getPhysicalNumberOfRows();
            //遍历数据从一行开始
            for (int i = 1; i < numberOfRows; i++) {
                XSSFRow row = sheetAt.getRow(i);
                //获取第一行第一列数据
               Date date = row.getCell(0).getDateCellValue();
               //将date转为locadate类型
                LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                //获取第一行第二列数据
                double number = row.getCell(1).getNumericCellValue();
                //将从文件读取出来的数据存入对象
                Ordersetting ordersetting = new Ordersetting(localDate,(int)number);
                //将对象存入集合
                ordersettingList.add(ordersetting);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ordersettingList;
    }




}

